home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / tar_110.lha / tar-1.10 / list.c < prev    next >
C/C++ Source or Header  |  1991-06-21  |  17KB  |  722 lines

  1. /* List a tar archive.
  2.    Copyright (C) 1988 Free Software Foundation
  3.  
  4. This file is part of GNU Tar.
  5.  
  6. GNU Tar is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Tar is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Tar; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /*
  21.  * List a tar archive.
  22.  *
  23.  * Also includes support routines for reading a tar archive.
  24.  *
  25.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  26.  *
  27.  * @(#)list.c 1.31 11/5/87 - gnu
  28.  */
  29. #include <stdio.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #ifndef    MSDOS
  34. #include <sys/file.h>
  35. #endif    /* MSDOS */
  36.  
  37. #ifdef USG
  38. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  39. #endif
  40.  
  41. char *ctime();                /* From libc.a */
  42.  
  43. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  44.  
  45. #include "tar.h"
  46. #include "port.h"
  47.  
  48. extern FILE *msg_file;
  49.  
  50. long from_oct();            /* Decode octal number */
  51. void demode();                /* Print file mode */
  52.  
  53. union record *head;            /* Points to current archive header */
  54. struct stat hstat;            /* Stat struct corresponding */
  55. int head_standard;            /* Tape header is in ANSI format */
  56.  
  57. void print_header();
  58. void skip_file();
  59. void skip_extended_headers();
  60.  
  61. extern char *quote_copy_string();
  62.  
  63.  
  64. /*
  65.  * Main loop for reading an archive.
  66.  */
  67. void
  68. read_and(do_something)
  69.     void (*do_something)();
  70. {
  71.     int status = 3;            /* Initial status at start of archive */
  72.     int prev_status;
  73.     extern time_t new_time;
  74.     char save_linkflag;
  75.     
  76.     name_gather();            /* Gather all the names */
  77.     open_archive(1);        /* Open for reading */
  78.  
  79.     for(;;) {
  80.         prev_status = status;
  81.         status = read_header();
  82.         switch (status) {
  83.  
  84.         case 1:            /* Valid header */
  85.             /* We should decode next field (mode) first... */
  86.             /* Ensure incoming names are null terminated. */
  87.             head->header.name[NAMSIZ-1] = '\0';
  88.  
  89.             if (   !name_match(head->header.name)
  90.                  || (f_new_files && hstat.st_mtime<new_time)
  91.                  || (f_exclude && check_exclude(head->header.name))) {
  92.  
  93.                 int isextended = 0;
  94.  
  95.                 if(   head->header.linkflag==LF_VOLHDR
  96.                     || head->header.linkflag==LF_MULTIVOL
  97.                    || head->header.linkflag==LF_NAMES) {
  98.                     (*do_something)();
  99.                     continue;
  100.                 }
  101.                 /* Skip past it in the archive */
  102.                 if (head->header.isextended)
  103.                     isextended = 1;
  104.                 save_linkflag = head->header.linkflag;
  105.                 userec(head);
  106.                 if (isextended) {
  107. /*                    register union record *exhdr;
  108.  
  109.                     for (;;) {
  110.                         exhdr = findrec();
  111.                         if (!exhdr->ext_hdr.isextended) {
  112.                             userec(exhdr);
  113.                             break;
  114.                         }
  115.                     }
  116.                     userec(exhdr);*/
  117.                     skip_extended_headers();
  118.                 }
  119.                 /* Skip to the next header on the archive */
  120.                 if(save_linkflag != LF_DIR)
  121.                     skip_file((long)hstat.st_size);
  122.                 continue;
  123.  
  124.             }
  125.  
  126.             (*do_something)();
  127.             continue;
  128.  
  129.             /*
  130.              * If the previous header was good, tell them
  131.              * that we are skipping bad ones.
  132.              */
  133.         case 0:            /* Invalid header */
  134.             userec(head);
  135.             switch (prev_status) {
  136.             case 3:        /* Error on first record */
  137.                 msg("Hmm, this doesn't look like a tar archive.");
  138.                 /* FALL THRU */
  139.             case 2:        /* Error after record of zeroes */
  140.             case 1:        /* Error after header rec */
  141.                 msg("Skipping to next file header...");
  142.             case 0:        /* Error after error */
  143.                 break;
  144.             }
  145.             continue;
  146.  
  147.         case 2:            /* Record of zeroes */
  148.             userec(head);
  149.             status = prev_status;    /* If error after 0's */
  150.             if (f_ignorez)    
  151.                 continue;
  152.             /* FALL THRU */
  153.         case EOF:        /* End of archive */
  154.             break;
  155.         }
  156.         break;
  157.     };
  158.  
  159.     close_archive();
  160.     names_notfound();        /* Print names not found */
  161. }        
  162.  
  163.  
  164. /*
  165.  * Print a header record, based on tar options.
  166.  */
  167. void
  168. list_archive()
  169. {
  170.     extern char *save_name;
  171.     int    isextended = 0; /* Flag to remember if head is extended */
  172.     
  173.     /* Save the record */
  174.     saverec(&head);
  175.  
  176.     /* Print the header record */
  177.     if (f_verbose) {
  178.         if (f_verbose > 1)
  179.             decode_header(head, &hstat, &head_standard, 0);
  180.         print_header();
  181.     }
  182.  
  183.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  184.         size_t    size, written, check;
  185.         char    *data;
  186.         extern int errno;
  187.         extern long save_totsize;
  188.         extern long save_sizeleft;
  189.  
  190.         userec(head);
  191.         if(f_multivol) {
  192.             save_name = head->header.name;
  193.             save_totsize=hstat.st_size;
  194.         }
  195.         for(size = hstat.st_size;size>0;size-=written) {
  196.             if(f_multivol)
  197.                 save_sizeleft=size;
  198.             data = findrec()->charptr;
  199.             if(data==NULL) {
  200.                 msg("EOF in archive file?");
  201.                 break;
  202.             }
  203.             written = endofrecs()->charptr - data;
  204.             if(written>size)
  205.                 written=size;
  206.             errno=0;
  207.             check=fwrite(data,sizeof(char), written, msg_file);
  208.             userec((union record *)(data+written - 1));
  209.             if(check!=written) {
  210.                 msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  211.                 skip_file((long)(size)-written);
  212.                 break;
  213.             }
  214.         }
  215.         if(f_multivol)
  216.             save_name = 0;
  217.         saverec((union record **) 0);    /* Unsave it */
  218.         fputc('\n',msg_file);
  219.         fflush(msg_file);
  220.         return;
  221.  
  222.     }
  223.     saverec((union record **) 0);    /* Unsave it */
  224.     /* Check to see if we have an extended header to skip over also */
  225.     if (head->header.isextended) 
  226.         isextended = 1;
  227.         
  228.     /* Skip past the header in the archive */
  229.     userec(head);
  230.  
  231.     /*
  232.       * If we needed to skip any extended headers, do so now, by
  233.       * reading extended headers and skipping past them in the 
  234.      * archive.
  235.      */
  236.     if (isextended) {
  237. /*        register union record *exhdr;
  238.  
  239.         for (;;) {
  240.             exhdr = findrec();
  241.  
  242.             if (!exhdr->ext_hdr.isextended) {
  243.                 userec(exhdr);
  244.                 break;
  245.             }
  246.             userec(exhdr);
  247.         }*/
  248.         skip_extended_headers();
  249.     }
  250.             
  251.     if(f_multivol)
  252.         save_name=head->header.name;
  253.     /* Skip to the next header on the archive */
  254.         
  255.     skip_file((long) hstat.st_size);
  256.         
  257.     if(f_multivol)
  258.         save_name = 0;
  259. }
  260.  
  261.  
  262. /*
  263.  * Read a record that's supposed to be a header record.
  264.  * Return its address in "head", and if it is good, the file's
  265.  * size in hstat.st_size.
  266.  *
  267.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  268.  * 2 for a record full of zeros (EOF marker).
  269.  *
  270.  * You must always userec(head) to skip past the header which this
  271.  * routine reads.
  272.  */
  273. int
  274. read_header()
  275. {
  276.     register int    i;
  277.     register long    sum, recsum;
  278.     register char    *p;
  279.     register union record *header;
  280.     long    from_oct();
  281.  
  282.     header = findrec();
  283.     head = header;        /* This is our current header */
  284.     if (NULL == header)
  285.         return EOF;
  286.  
  287.     recsum = from_oct(8,  header->header.chksum);
  288.  
  289.     sum = 0;
  290.     p = header->charptr;
  291.     for (i = sizeof(*header); --i >= 0;) {
  292.         /*
  293.          * We can't use unsigned char here because of old compilers,
  294.          * e.g. V7.
  295.          */
  296.         sum += 0xFF & *p++;
  297.     }
  298.  
  299.     /* Adjust checksum to count the "chksum" field as blanks. */
  300.     for (i = sizeof(header->header.chksum); --i >= 0;)
  301.         sum -= 0xFF & header->header.chksum[i];
  302.     sum += ' '* sizeof header->header.chksum;    
  303.  
  304.     if (sum == recsum) {
  305.         /*
  306.          * Good record.  Decode file size and return.
  307.          */
  308.         if (header->header.linkflag == LF_LINK)
  309.             hstat.st_size = 0;    /* Links 0 size on tape */
  310.         else
  311.             hstat.st_size = from_oct(1+12, header->header.size);
  312.         return 1;
  313.     }
  314.  
  315.     if (sum == 8*' ') {
  316.         /*
  317.          * This is a zeroed record...whole record is 0's except
  318.          * for the 8 blanks we faked for the checksum field.
  319.          */
  320.         return 2;
  321.     }
  322.  
  323.     return 0;
  324. }
  325.  
  326.  
  327. /* 
  328.  * Decode things from a file header record into a "struct stat".
  329.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  330.  * Standard" tar format or regular old tar format.
  331.  *
  332.  * read_header() has already decoded the checksum and length, so we don't.
  333.  *
  334.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  335.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  336.  *
  337.  * decode_header should NOT be called twice for the same record, since the
  338.  * two calls might use different "wantug" values and thus might end up with
  339.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  340.  * should decode it first, and other callers should decode it without uid/gid
  341.  * before calling a routine, e.g. print_header, that assumes decoded data.
  342.  */
  343. decode_header(header, st, stdp, wantug)
  344.     register union record    *header;
  345.     register struct stat    *st;
  346.     int    *stdp;
  347.     int    wantug;
  348. {
  349.  
  350.     long from_oct();
  351.  
  352.     st->st_mode = from_oct(8,  header->header.mode);
  353.     st->st_mtime = from_oct(1+12, header->header.mtime);
  354.     if(f_gnudump) {
  355.         st->st_atime = from_oct(1+12, header->header.atime);
  356.         st->st_ctime = from_oct(1+12, header->header.ctime);
  357.     }
  358.     
  359.     if (0==strcmp(header->header.magic, TMAGIC)) {
  360.         /* Unix Standard tar archive */
  361.         *stdp = 1;
  362.         if (wantug) {
  363. #ifdef NONAMES
  364.             st->st_uid = from_oct(8,  header->header.uid);
  365.             st->st_gid = from_oct(8,  header->header.gid);
  366. #else
  367.             st->st_uid = finduid(header->header.uname);
  368.             st->st_gid = findgid(header->header.gname);
  369. #endif
  370.         }
  371.         switch (header->header.linkflag) {
  372.         case LF_BLK: case LF_CHR:
  373.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  374.                        from_oct(8, header->header.devminor));
  375.         }
  376.     } else {
  377.         /* Old fashioned tar archive */
  378.         *stdp = 0;
  379.         st->st_uid = from_oct(8,  header->header.uid);
  380.         st->st_gid = from_oct(8,  header->header.gid);
  381.         st->st_rdev = 0;
  382.     }
  383. }
  384.  
  385.  
  386. /*
  387.  * Quick and dirty octal conversion.
  388.  *
  389.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  390.  */
  391. long
  392. from_oct(digs, where)
  393.     register int    digs;
  394.     register char    *where;
  395. {
  396.     register long    value;
  397.  
  398.     while (isspace(*where)) {        /* Skip spaces */
  399.         where++;
  400.         if (--digs <= 0)
  401.             return -1;        /* All blank field */
  402.     }
  403.     value = 0;
  404.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  405.         value = (value << 3) | (*where++ - '0');
  406.         --digs;
  407.     }
  408.  
  409.     if (digs > 0 && *where && !isspace(*where))
  410.         return -1;            /* Ended on non-space/nul */
  411.  
  412.     return value;
  413. }
  414.  
  415.  
  416. /*
  417.  * Actually print it.
  418.  *
  419.  * Plain and fancy file header block logging.
  420.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  421.  * This should just contain file names, so it can be fed back into tar
  422.  * with xargs or the "-T" option.  The verbose option can give a bunch
  423.  * of info, one line per file.  I doubt anybody tries to parse its
  424.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  425.  * anyway.
  426.  *
  427.  * Note that print_header uses the globals <head>, <hstat>, and
  428.  * <head_standard>, which must be set up in advance.  This is not very clean
  429.  * and should be cleaned up.  FIXME.
  430.  */
  431. #define    UGSWIDTH    11        /* min width of User, group, size */
  432. #define    DATEWIDTH    19        /* Last mod date */
  433. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  434.  
  435. void
  436. print_header()
  437. {
  438.     char modes[11];
  439.     char *timestamp;
  440.     char uform[11], gform[11];    /* These hold formatted ints */
  441.     char *user, *group;
  442.     char size[24];        /* Holds a formatted long or maj, min */
  443.     long longie;        /* To make ctime() call portable */
  444.     int    pad;
  445.     char *name;
  446.     extern long baserec;
  447.  
  448.     if(f_sayblock)
  449.         fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  450.     /* annofile(msg_file, (char *)NULL); */
  451.  
  452.     if (f_verbose <= 1) {
  453.         /* Just the fax, mam. */
  454.         char *name;
  455.  
  456.         name=quote_copy_string(head->header.name);
  457.         if(name==0)
  458.             name=head->header.name;
  459.         fprintf(msg_file, "%s\n", name);
  460.         if(name!=head->header.name)
  461.             free(name);
  462.     } else {
  463.         /* File type and modes */
  464.         modes[0] = '?';
  465.         switch (head->header.linkflag) {
  466.         case LF_VOLHDR:
  467.             modes[0]='V';
  468.             break;
  469.  
  470.         case LF_MULTIVOL:
  471.             modes[0]='M';
  472.             break;
  473.  
  474.         case LF_NAMES:
  475.             modes[0]='N';
  476.             break;
  477.  
  478.         case LF_SPARSE:
  479.         case LF_NORMAL:
  480.         case LF_OLDNORMAL:
  481.         case LF_LINK:
  482.                 modes[0] = '-'; 
  483.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  484.                     modes[0] = 'd';
  485.                 break;
  486.         case LF_DUMPDIR:modes[0] = 'd'; break;
  487.         case LF_DIR:    modes[0] = 'd'; break;
  488.         case LF_SYMLINK:modes[0] = 'l'; break;
  489.         case LF_BLK:    modes[0] = 'b'; break;
  490.         case LF_CHR:    modes[0] = 'c'; break;
  491.         case LF_FIFO:    modes[0] = 'p'; break;    
  492.         case LF_CONTIG:    modes[0] = 'C'; break;
  493.         }
  494.  
  495.         demode((unsigned)hstat.st_mode, modes+1);
  496.  
  497.         /* Timestamp */
  498.         longie = hstat.st_mtime;
  499.         timestamp = ctime(&longie);
  500.         timestamp[16] = '\0';
  501.         timestamp[24] = '\0';
  502.  
  503.         /* User and group names */
  504.         if (*head->header.uname && head_standard) {
  505.             user  = head->header.uname;
  506.         } else {
  507.             user = uform;
  508.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  509.         }
  510.         if (*head->header.gname && head_standard) {
  511.             group = head->header.gname;
  512.         } else {
  513.             group = gform;
  514.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  515.         }
  516.  
  517.         /* Format the file size or major/minor device numbers */
  518.         switch (head->header.linkflag) {
  519.         case LF_CHR:
  520.         case LF_BLK:
  521.             (void)sprintf(size, "%d,%d",
  522.                     major(hstat.st_rdev),
  523.                     minor(hstat.st_rdev));
  524.             break;
  525.         case LF_SPARSE:
  526.             (void)sprintf(size, "%ld",
  527.                  from_oct(1+12, head->header.realsize));
  528.             break;
  529.         default:
  530.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  531.         }
  532.  
  533.         /* Figure out padding and print the whole line. */
  534.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  535.         if (pad > ugswidth) ugswidth = pad;
  536.  
  537.         name = quote_copy_string(head->header.name);
  538.         if(!name)
  539.             name=head->header.name;
  540.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %.*s",
  541.             modes,
  542.             user,
  543.             group,
  544.             ugswidth - pad,
  545.             "",
  546.             size,
  547.             timestamp+4, timestamp+20,
  548.             sizeof(head->header.name),
  549.             name);
  550.  
  551.         if(name!=head->header.name)
  552.             free(name);
  553.         switch (head->header.linkflag) {
  554.         case LF_SYMLINK:
  555.             name=quote_copy_string(head->header.linkname);
  556.             if(!name)
  557.                 name=head->header.linkname;
  558.             fprintf(msg_file, " -> %s\n", name);
  559.             if(name!=head->header.linkname)
  560.                 free(name);
  561.             break;
  562.  
  563.         case LF_LINK:
  564.             name=quote_copy_string(head->header.linkname);
  565.             if(!name)
  566.                 name=head->header.linkname;
  567.             fprintf(msg_file, " link to %s\n", head->header.linkname);
  568.             if(name!=head->header.linkname)
  569.                 free(name);
  570.             break;
  571.  
  572.         default:
  573.             fprintf(msg_file, " unknown file type '%c'\n",
  574.                 head->header.linkflag);
  575.             break;
  576.  
  577.         case LF_OLDNORMAL:
  578.         case LF_NORMAL:
  579.         case LF_SPARSE:
  580.         case LF_CHR:
  581.         case LF_BLK:
  582.         case LF_DIR:
  583.         case LF_FIFO:
  584.         case LF_CONTIG:
  585.         case LF_DUMPDIR:
  586.             putc('\n', msg_file);
  587.             break;
  588.  
  589.         case LF_VOLHDR:
  590.             fprintf(msg_file, "--Volume Header--\n");
  591.             break;
  592.             
  593.         case LF_MULTIVOL:
  594.             fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  595.             break;
  596.  
  597.         case LF_NAMES:
  598.             fprintf(msg_file,"--Mangled file names--\n");
  599.             break;
  600.         }
  601.     }
  602.     fflush(msg_file);
  603. }
  604.  
  605. /*
  606.  * Print a similar line when we make a directory automatically.
  607.  */
  608. void
  609. pr_mkdir(pathname, length, mode)
  610.     char *pathname;
  611.     int length;
  612.     int mode;
  613. {
  614.     char modes[11];
  615.     char *name;
  616.     extern long baserec;
  617.  
  618.     if (f_verbose > 1) {
  619.         /* File type and modes */
  620.         modes[0] = 'd';
  621.         demode((unsigned)mode, modes+1);
  622.  
  623.         if(f_sayblock)
  624.             fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  625.         /* annofile(msg_file, (char *)NULL); */
  626.         name=quote_copy_string(pathname);
  627.         if(!name)
  628.             name=pathname;
  629.         fprintf(msg_file, "%s %*s %.*s\n",
  630.             modes,
  631.             ugswidth+DATEWIDTH,
  632.             "Creating directory:",
  633.             length,
  634.             pathname);
  635.         if(name!=pathname)
  636.             free(name);
  637.     }
  638. }
  639.  
  640.  
  641. /*
  642.  * Skip over <size> bytes of data in records in the archive.
  643.  */
  644. void
  645. skip_file(size)
  646.     register long size;
  647. {
  648.     union record *x;
  649.     extern long save_totsize;
  650.     extern long save_sizeleft;
  651.  
  652.     if(f_multivol) {
  653.         save_totsize=size;
  654.         save_sizeleft=size;
  655.     }
  656.  
  657.     while (size > 0) {
  658.         x = findrec();
  659.         if (x == NULL) {    /* Check it... */
  660.             msg("Unexpected EOF on archive file");
  661.             exit(EX_BADARCH);
  662.         }
  663.         userec(x);
  664.         size -= RECORDSIZE;
  665.         if(f_multivol)
  666.             save_sizeleft-=RECORDSIZE;
  667.     }
  668. }
  669.  
  670. void
  671. skip_extended_headers()
  672. {
  673.     register union record *exhdr;
  674.  
  675.     for (;;) {
  676.         exhdr = findrec();
  677.         if (!exhdr->ext_hdr.isextended) {
  678.             userec(exhdr);
  679.             break;
  680.         }
  681.         userec (exhdr);
  682.     }
  683. }
  684.  
  685. /*
  686.  * Decode the mode string from a stat entry into a 9-char string and a null.
  687.  */
  688. void
  689. demode(mode, string)
  690.     register unsigned mode;
  691.     register char *string;
  692. {
  693.     register unsigned mask;
  694.     register char *rwx = "rwxrwxrwx";
  695.  
  696.     for (mask = 0400; mask != 0; mask >>= 1) {
  697.         if (mode & mask)
  698.             *string++ = *rwx++;
  699.         else {
  700.             *string++ = '-';
  701.             rwx++;
  702.         }
  703.     }
  704.  
  705.     if (mode & S_ISUID)
  706.         if (string[-7] == 'x')
  707.             string[-7] = 's';
  708.         else
  709.             string[-7] = 'S';
  710.     if (mode & S_ISGID)
  711.         if (string[-4] == 'x')
  712.             string[-4] = 's';
  713.         else
  714.             string[-4] = 'S';
  715.     if (mode & S_ISVTX)
  716.         if (string[-1] == 'x')
  717.             string[-1] = 't';
  718.         else
  719.             string[-1] = 'T';
  720.     *string = '\0';
  721. }
  722.